package com.ops.controller;

import com.ops.entity.OpsTask;
import com.ops.service.OpsTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运维任务控制器
 * 处理运维任务的导入、查询和统计等操作
 */
@RestController
@RequestMapping("/api/tasks")
public class OpsTaskController {
    
    @Autowired
    private OpsTaskService opsTaskService;
    
    /**
     * 导入运维任务数据
     * 支持Excel文件导入运维任务记录
     */
    @PostMapping("/import")
    public ResponseEntity<?> importExcel(
        @RequestParam("file") MultipartFile file,
        @RequestParam("hospitalId") String hospitalId,
        @RequestParam("demandYear") String demandYear,
        @RequestParam("reportUuid") String reportUuid
    ) {
        try {
            int count = opsTaskService.importExcel(file, hospitalId, demandYear, reportUuid);
            Map<String, Object> response = new HashMap<>();
            response.put("count", count);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("导入失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取月度统计数据
     * 统计每月的运维需求情况
     */
    @GetMapping("/{reportUuid}/{hospitalId}/{demandYear}/monthly-stats")
    public ResponseEntity<List<Map<String, Object>>> getMonthlyStats(
        @PathVariable String reportUuid,
        @PathVariable String hospitalId,
        @PathVariable String demandYear
    ) {
        return ResponseEntity.ok(opsTaskService.getMonthlyDemandStats(reportUuid, hospitalId, demandYear));
    }
    
    /**
     * 分页获取运维任务
     * 支持分页查询运维任务记录
     */
    @GetMapping("/{reportUuid}/{hospitalId}/{demandYear}/page")
    public ResponseEntity<Page<OpsTask>> getTasksPage(
        @PathVariable String reportUuid,
        @PathVariable String hospitalId,
        @PathVariable String demandYear,
        @RequestParam(defaultValue = "0") int page,
        @RequestParam(defaultValue = "3") int size
    ) {
        Page<OpsTask> tasks = opsTaskService.findTopTasks(reportUuid, hospitalId, demandYear, page, size);
        return ResponseEntity.ok(tasks);
    }
    
    /**
     * 获取月度工作量统计
     * 统计每月的工作量情况
     */
    @GetMapping("/{reportUuid}/{hospitalId}/{demandYear}/monthly-workload")
    public ResponseEntity<List<Map<String, Object>>> getMonthlyWorkload(
        @PathVariable String reportUuid,
        @PathVariable String hospitalId,
        @PathVariable String demandYear
    ) {
        return ResponseEntity.ok(opsTaskService.getMonthlyWorkloadStats(reportUuid, hospitalId, demandYear));
    }
} 